// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
/**
 * 初始启动界面
 * 功能：获取连接地址，初始配置信息
 */

cc.Class({
    extends: cc.Component,

    properties: {
        loginBar:cc.ProgressBar,//进度条
        labDec:cc.Label,
        labLog:cc.Label,
        ndRet:cc.Node,//弹出提示
        _isUpdate:-1,//不更
        _prss:0,
        _timer:0,
        _decStr:'',

        login:false,
    },

    onReconnect:function(){
        this.ndRet.active = false;
        this.appStart();
    },

    appStart:function(){
        var self = this;
        var initServer = function(ret){
            if(ret.Code == -1 && self.labLog)
            {
                self.labLog.string = "连接服务器失败...";
                self.appStart();
                return;
            }
            self._isUpdate = ret.IsUpdate;//1强更 or 0热更 or -1不更
            CL.HTTP.url = ret.GatewayUrl;//网关地址
            CL.HTTP.hotUrl = ret.UpdateUrl;//强更地址
            CL.NET.ip = ret.SocketURL;//长连地址
            User.setAppState(ret.Status);
            //"热更地址:" -> ret.HotUpdateUrl
            if(!cc.sys.isNative)
            {
                self._isUpdate = -1;
            }

            self.labLog.string = self._decStr+"服务器连接成功";
            self.login = true;
           
        };

        let rcount = 0;//重连次数
        var xhr = null;
        var complete = false;

        var fnRequest = function(){
        
            if(rcount>=3)
            {
                rcount = 0;
                self.ndRet.active = true;
                return;
            }

            self.labLog.string = self._decStr+"正在连接服务器...";
            cc.log("正在连接服务器版本号：" + CL.HTTP.version );
            var data = {
                Equipment: Utils.getEquipment(),
                Version: CL.HTTP.version,
            };
            xhr = CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.STARTAPP,data,function(ret){
                xhr = null;
                complete = true;
                initServer(ret);
            },"POST");
            setTimeout(fn,3000);        
            rcount++;    
        }
        
        var fn = function(){
            if(!complete){
                if(xhr){  
                   xhr.abort();
                    setTimeout(function(){
                        fnRequest();
                    },3000);
                }
                else{
                    fnRequest();
                }
            }
        };
        fn();
    },

    startGame:function(){
        //预加载资源
        ComponentsUtils.preloadRes();
        cc.director.loadScene("MainScene");
    },

    //初始化公共对象
    init:function(){
        CL.HTTP = require("Http");
        CL.NET = require("Net");
        CL.SOUND = require("SoundManage");
        CL.SOUND.loadAudio(); 

        this.appStart();
    },

    // onLoad () {},

    start: function() {
        cc.director.setDisplayStats(false);
        this._decStr = '';
        var frist = cc.sys.localStorage.getItem("fristGame");
        if(typeof frist != "string"){

            this._decStr = '首次载入场景时间会长一些...\n';
            cc.sys.localStorage.setItem('fristGame','0');
        }
        else
        {
           if(frist != '0')
           {
                this._decStr = '首次载入场景时间会长一些...\n';
           }
        }

        this.init();

    },

    update: function(dt){
        this._timer += dt;
        if ( this._timer >= 1.0 ) {
            this._timer = 0;
        }

        if(this._prss<100)
        { 
            this._prss += 1;
            this.labDec.string = this._prss + '%';
            this.loginBar.progress = (this._prss/100).toFixed(1); 
        }
        if(this._prss == 100 && this.login)
        {
            this.login = false;
            this.startGame();
        }

    }
});
